//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using Microsoft.TridentWordAddIn.Server;
using Microsoft.TridentWordAddIn.ResourceHelper;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// 
    /// </summary>
    internal partial class SqlStoreInformation : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SqlStoreInformation"/> class.
        /// </summary>
        public SqlStoreInformation()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Occurs when [on validation].
        /// </summary>
        public event EventHandler<ConnectionValidateEventArgs> OnValidation;

        /// <summary>
        /// Occurs when [on database clicked].
        /// </summary>
        public event EventHandler<DatabaseClickedEventArgs> OnDatabaseClicked;

        /// <summary>
        /// Gets or sets the name of the database.
        /// </summary>
        /// <value>The name of the database.</value>
        public string DatabaseName
        {
            get
            {
                return this.cmbDatabaseName.Text;
            }
            set
            {
                this.cmbDatabaseName.Text = value;
            }
        }

        /// <summary>
        /// Handles the DropDown event of the cmbDBName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cmbDatabaseName_DropDown(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                DatabaseClickedEventArgs databaseEventArgs = new DatabaseClickedEventArgs();
                this.OnDatabaseClicked(this, databaseEventArgs);

                this.cmbDatabaseName.Items.Clear();
                this.cmbDatabaseName.Items.AddRange(databaseEventArgs.DatabaseList);
            }
            catch (Exception)
            {
                MessageBoxHelper.ShowDefault(ResourceFetcher.GetString("DBListFailed"), MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        /// <summary>
        /// Determines whether [is valid data base name].
        /// </summary>
        /// <returns></returns>
        private ValidationResult IsValidDataBaseName()
        {
            ValidationResult result = new ValidationResult(!cmbDatabaseName.Text.Trim().IsNullOrEmpty());
            if (!result.IsValid)
            {
                result.Messages.Add("Database name is required");
            }

            return result;
        }

        /// <summary>
        /// Handles the Validating event of the cmbDBName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void cmbDatabaseName_Validating(object sender, CancelEventArgs e)
        {
            if (this.OnValidation != null)
            {
                ValidationResult result = this.IsValidDataBaseName();
                ConnectionValidateEventArgs validateEventArgs = new ConnectionValidateEventArgs(
                    result.GetMessage(),
                    result.IsValid,
                    "ComboBox");

                this.OnValidation(cmbDatabaseName, validateEventArgs);
            }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <returns></returns>
        public ValidationResult IsValid()
        {
            ValidationResult result = this.IsValidDataBaseName();
            if (!result.IsValid)
            {
                this.cmbDatabaseName_Validating(cmbDatabaseName, null);
            }

            return result;
        }
    }
}
